var toAppointmentCountCl

export default new Vuex.Store({
  state: {
    toAppointmentVisible: false,
    houseId: undefined,
    category: undefined,
    // 全局搜索关键字
    searchKeyword: '',
    // 全局搜索的Tab
    searchTab: '',
    // 点击发送验证码后的倒计时
    toAppointmentCount: 0
  },
  mutations: {
    showAppointmentVisible (state) {
      state.toAppointmentVisible = true
    },
    changeHouseId (state, houseId) {
      state.houseId = houseId
    },
    changeHouseCategory (state, category) {
      state.category = category
    },
    hideAppointmentVisible (state) {
      state.toAppointmentVisible = false
    },
    changeSearchKeyword (state, keyword) {
      state.searchKeyword = keyword
    },
    changeSearchTab (state, tab) {
      state.searchTab = tab
    },
    setToAppointmentCount (state, count) {
      state.toAppointmentCount = count
    }
  },
  actions: {
    subtractionCountDown (context) {
      // 如果倒计时还在继续，则阻止继续执行
      if (context.state.toAppointmentCount > 0) {
        return
      }
      // 默认为60秒
      let count = 60
      context.commit('setToAppointmentCount', count)

      // 清除之前可能的定时器
      clearInterval(toAppointmentCountCl)

      // 开始新的定时器
      toAppointmentCountCl = setInterval(() => {
        if (count < 1) {
          clearInterval(toAppointmentCountCl)
          return
        }
        count = count - 1
        context.commit('setToAppointmentCount', count)
      }, 1000)
    },
<<<<<<< HEAD
    /*stopCountDown (){
      clearInterval(toAppointmentCountCl)
    }*/
=======
    stopCountDown () {
      // 清除之前可能的定时器
      clearInterval(toAppointmentCountCl)
    }
>>>>>>> 30884f52a02220269ee408b0f4b318f270b86cf9
  }
})
